using System;
using Microsoft.Xna.Framework;
using XnaPlus.Common.Extensions;

namespace FlipRunner.Physics
{
    public static class PhysicsComponentExtensions
    {
        public static Rectangle GetDrawRectangle(this IPhysicsComponent component)
        {
            return GetCartesianHitBox(component).ToPixels(component.Game.PixelsPerMeter, component.Game.PixelBounds.Height);
        }

        public static float Right(this IPhysicsComponent component)
        {
            switch (component.Anchor)
            {
                case PositionAnchor.Center:
                    return component.Position.X + component.Size.Width/2;
                case PositionAnchor.TopLeft:
                    return component.Position.X + component.Size.Width;
                default:
                    throw new NotImplementedException();
            }
        }

        public static float Left(this IPhysicsComponent component)
        {
            switch (component.Anchor)
            {
                case PositionAnchor.Center:
                    return component.Position.X - component.Size.Width / 2;
                case PositionAnchor.TopLeft:
                    return component.Position.X;
                default:
                    throw new NotImplementedException();
            }
        }

        public static float Bottom(this IPhysicsComponent component)
        {
            switch (component.Anchor)
            {
                case PositionAnchor.Center:
                    return component.Position.Y - component.Size.Height / 2;
                case PositionAnchor.TopLeft:
                    return component.Position.Y - component.Size.Height;
                default:
                    throw new NotImplementedException();
            }
        }

        public static float Top(this IPhysicsComponent component)
        {
            switch (component.Anchor)
            {
                case PositionAnchor.Center:
                    return component.Position.Y + component.Size.Height / 2;
                case PositionAnchor.TopLeft:
                    return component.Position.Y;
                default:
                    throw new NotImplementedException();
            }
        }

        public static bool IsOutsideWorldBounds(this IPhysicsComponent component)
        {
            var hitbox = component.GetCartesianHitBox();
            var worldSize = component.Game.WorldSize;
            return hitbox.Left <= 0 || hitbox.Bottom <= 0 || hitbox.Right >= worldSize.Width ||
                   hitbox.Top >= worldSize.Height;
        }

        public static bool IsCompletelyOutsideWorldBounds(this IPhysicsComponent component)
        {
            var hitbox = component.GetCartesianHitBox();
            var worldSize = component.Game.WorldSize;
            return hitbox.Right < 0 || hitbox.Top < 0 || hitbox.Left > worldSize.Width ||
                   hitbox.Bottom > worldSize.Height;
        }

        public static CartesianRectangle GetCartesianHitBox(this IPhysicsComponent component)
        {
            return new CartesianRectangle(component.Left(), component.Bottom(), component.Size.Width, component.Size.Height);
        }
    }
}